package com.cyzy.service.impl;

import com.cyzy.entity.TeacherAvailableTime;
import com.cyzy.exception.BusinessException;
import com.cyzy.mapper.TeacherAvailableTimeMapper;
import com.cyzy.service.TeacherAvailableTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherAvailableTimeServiceImpl implements TeacherAvailableTimeService {

    @Autowired
    private TeacherAvailableTimeMapper teacherAvailableTimeMapper;

    @Override
    public Map<String, Object> getList(String keyword, String teacherName, int pageNum, int pageSize) {
        // 计算分页起始位置
        int start = (pageNum - 1) * pageSize;

        // 查询课程列表和总记录数
        List<TeacherAvailableTime> list = teacherAvailableTimeMapper.selectList(keyword, teacherName, start, pageSize);
        int total = teacherAvailableTimeMapper.selectCount(keyword, teacherName);

        // 封装结果
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);

        return result;
    }

    @Override
    public TeacherAvailableTime getById(Integer id) {
        if (id == null) {
            throw new BusinessException("课程ID不能为空");
        }
        return teacherAvailableTimeMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(TeacherAvailableTime teacherAvailableTime) {
        // 参数校验
        if (teacherAvailableTime == null) {
            throw new BusinessException("课程信息不能为空");
        }

        if (teacherAvailableTime.getTeacherId() == null) {
            throw new BusinessException("教师ID不能为空");
        }

        if (teacherAvailableTime.getTeacherName() == null || teacherAvailableTime.getTeacherName().trim().isEmpty()) {
            throw new BusinessException("教师姓名不能为空");
        }

        if (teacherAvailableTime.getCourseName() == null || teacherAvailableTime.getCourseName().trim().isEmpty()) {
            throw new BusinessException("课程名称不能为空");
        }

        if (teacherAvailableTime.getWeekday() == null || teacherAvailableTime.getWeekday() < 1 || teacherAvailableTime.getWeekday() > 6) {
            throw new BusinessException("星期几必须在1-6之间");
        }

        if (teacherAvailableTime.getTimePeriod() == null || (teacherAvailableTime.getTimePeriod() != 1 && teacherAvailableTime.getTimePeriod() != 2)) {
            throw new BusinessException("时间段必须是1(上午)或2(下午)");
        }

        // 执行新增操作
        int rows = teacherAvailableTimeMapper.insert(teacherAvailableTime);
        return rows > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(TeacherAvailableTime teacherAvailableTime) {
        // 参数校验
        if (teacherAvailableTime == null || teacherAvailableTime.getId() == null) {
            throw new BusinessException("课程ID不能为空");
        }

        // 验证课程是否存在
        TeacherAvailableTime existing = teacherAvailableTimeMapper.selectById(teacherAvailableTime.getId());
        if (existing == null) {
            throw new BusinessException("课程不存在");
        }

        // 验证其他参数
        if (teacherAvailableTime.getTeacherId() == null) {
            throw new BusinessException("教师ID不能为空");
        }

        if (teacherAvailableTime.getTeacherName() == null || teacherAvailableTime.getTeacherName().trim().isEmpty()) {
            throw new BusinessException("教师姓名不能为空");
        }

        if (teacherAvailableTime.getCourseName() == null || teacherAvailableTime.getCourseName().trim().isEmpty()) {
            throw new BusinessException("课程名称不能为空");
        }

        if (teacherAvailableTime.getWeekday() == null || teacherAvailableTime.getWeekday() < 1 || teacherAvailableTime.getWeekday() > 6) {
            throw new BusinessException("星期几必须在1-6之间");
        }

        if (teacherAvailableTime.getTimePeriod() == null || (teacherAvailableTime.getTimePeriod() != 1 && teacherAvailableTime.getTimePeriod() != 2)) {
            throw new BusinessException("时间段必须是1(上午)或2(下午)");
        }

        // 执行更新操作
        int rows = teacherAvailableTimeMapper.update(teacherAvailableTime);
        return rows > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        // 参数校验
        if (id == null) {
            throw new BusinessException("课程ID不能为空");
        }

        // 验证课程是否存在
        TeacherAvailableTime existing = teacherAvailableTimeMapper.selectById(id);
        if (existing == null) {
            throw new BusinessException("课程不存在");
        }

        // 执行删除操作
        int rows = teacherAvailableTimeMapper.deleteById(id);
        return rows > 0;
    }
}
